package com.xin.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xin.entity.crm.DeptEntity;
import com.xin.entity.crm.EmployEntity;
import com.xin.exception.MyException;
import com.xin.mapper.DeptMapper;
import com.xin.service.DeptService;
import com.xin.service.EmployeeService;
import com.xin.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
/**
 * 部门
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, DeptEntity> implements DeptService {
    /**
     * 注入员工业务类
     */
    @Resource
    private EmployeeService employeeService;
    /**
     * 查询所有
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<DeptEntity> findList(Integer page, Integer size) {
        Page<DeptEntity> pages = new Page<>(page,size);
        Page<DeptEntity> deptEntityPage = this.baseMapper.selectPage(pages, null);
        List<DeptEntity> records = deptEntityPage.getRecords();

        return deptEntityPage;
    }

    /**
     * 根据部门名称模糊查询
     * @param name
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<DeptEntity> findByNameLike(String name, Integer page, Integer size) {

        Page<DeptEntity> pages = new Page<>(page,size);

        //设置总的条数
        pages.setTotal(this.baseMapper.selectCount(new QueryWrapper<DeptEntity>()
                .like("name",name)));
        //设置数据
        pages.setRecords(null);

        return pages;
    }

    /**
     * 添加部门
     * @param deptEntity
     * @return
     */
    @Override
    @Transactional
    public Integer add(DeptEntity deptEntity) {
        //校验部门名称是否重复
        DeptEntity item = this.baseMapper.selectOne(
                new QueryWrapper<DeptEntity>().eq("name", deptEntity.getName())
        );
        if (item!=null) throw new MyException("部门名称重复");
        //校验部门数据
        this.checkDeptData(deptEntity);
        Integer add = this.baseMapper.insert(deptEntity);
        return add;
    }

    /**
     * 修改部门
     * @param deptEntity
     * @return
     */
    @Override
    @Transactional
    public Integer update(DeptEntity deptEntity) {
        //校验部门名称是否重复
        DeptEntity item = this.baseMapper.selectOne(new QueryWrapper<DeptEntity>()
                .eq("name", deptEntity.getName())
                .ne("id", deptEntity.getId()));
        if (item!=null) throw new MyException("部门名称重复");
        //校验部门数据
        this.checkDeptData(deptEntity);
        Integer update = this.baseMapper.updateById(deptEntity);
        return update;
    }

    /**
     * 删除部门
     * @param deptId
     * @return
     */
    @Override
    @Transactional
    public Integer delete(Long deptId) {
        //查询该部门信息
        DeptEntity nowDept = this.baseMapper.selectOne(new QueryWrapper<DeptEntity>().eq("id", deptId));
        if (nowDept==null) throw new MyException("部门信息不存在,或者该部门已经被删除了");
        //校验该部门是否含有下级部门
        DeptEntity parentDept = this.baseMapper.selectOne(new QueryWrapper<DeptEntity>().eq("parent_id", deptId));
        if (parentDept!=null) throw new MyException("该部门下含有子部门无法删除");
        //校验部门里面是否含有员工信息
        List<EmployEntity> employEntityList = this.employeeService.getBaseMapper().selectList(new QueryWrapper<EmployEntity>().eq("department_id", deptId));
        if (employEntityList!=null&&employEntityList.size()>=1) throw new MyException("部门下存在员工无法删除该部门");

        return this.baseMapper.deleteById(deptId);
    }

    /**
     * 校验部门数据
     * @param deptEntity
     */
    @Override
    @Transactional
    public void checkDeptData(DeptEntity deptEntity) {
        //校验上级部门是否存在
        if (deptEntity.getParentId()!=null&&deptEntity.getParentId().longValue()!=0){
            DeptEntity parentId = this.baseMapper.selectOne(new QueryWrapper<DeptEntity>().eq("parent_id", deptEntity.getParentId()));
            if (parentId==null) throw new MyException("上级部门信息不存在");
        }
        //校验上级部门是否已经绑定了一个直接的下级部门
        DeptEntity parent_id = this.baseMapper.selectOne(new QueryWrapper<DeptEntity>().eq("parent_id", deptEntity.getParentId()));
        if (parent_id!=null) throw new MyException("上级部门已经有子部门了无法绑定");
        //校验部门路径是否正确
        String path = deptEntity.getPath();
        if (path!=null&&path.length()>0){
            List<Long> deptIdsFromPath = StringUtils.getDeptIdsFromPath(path);
            Integer count = this.baseMapper.selectCount(new QueryWrapper<DeptEntity>().in("id", deptIdsFromPath));
            if (count!=deptIdsFromPath.size()) throw new MyException("部门路径错误，里面含有了不存在的部门信息");
        }
        //校验部门经理是否存在
        EmployEntity employEntity = this.employeeService.getBaseMapper().selectOne(new QueryWrapper<EmployEntity>().eq("id", deptEntity.getParentId()));
        if (employEntity==null) throw new MyException("部门经理信息不存在");
    }
}
